﻿namespace Best.SmartCms.Dto.Response
{
    /// <summary>
    /// WebApi返回基础类
    /// </summary>
    public class ApiResponse : IResponse
    {
        /// <inheritdoc cref="IResponse"/>
        public bool Success { get; set; }

        /// <inheritdoc cref="IResponse"/>
        public ResponseCode Code { get; set; }

        /// <inheritdoc cref="IResponse"/>
        public string Message { get; set; }

        /// <summary>
        /// 成功结果
        /// </summary>
        /// <returns></returns>
        public static ApiResponse Succeeds()
        {
            return new ApiResponse() { Success = true, Code = ResponseCode.Success };
        }
        
        /// <summary>
        /// 失败结果
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="code">错误码</param>
        /// <returns></returns>
        public static ApiResponse Fails(string message, ResponseCode code)
        {
            return new ApiResponse() { Success = false, Message = message, Code = code };
        }
    }

    /// <summary>
    /// WebApi返回带实体数据类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiResponse<T> : ApiResponse
    {
        /// <summary>
        /// 业务数据
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 成功结果
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static ApiResponse<T> Succeeds(T value)
        {
            return new ApiResponse<T>() { Success = true, Code = ResponseCode.Success, Data = value};
        }

        /// <summary>
        /// 失败结果
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <param name="code">错误码</param>
        /// <returns></returns>
        public new static ApiResponse<T> Fails(string message, ResponseCode code)
        {
            return new ApiResponse<T>() { Success = false, Message = message, Code = code };
        }
    }
}